<?php

namespace App\Http\Controllers;

use App\Events\PostViewed;
use App\Jobs\PostViewsIncrement;
use Illuminate\Http\Request;
use App\Repos\PostRepo;

class PostController extends Controller
{

    protected $postRepo;

    public function __construct(PostRepo $postRepo)
    {
        $this->postRepo = $postRepo;
    }
    
    public function show($id)
    {
        //获取文章
        $post = $this->postRepo->getById($id);
        //阅读数+1
        // $views = $this->postRepo->addViews($post);
        //发布队列任务（显式队列）
        // $this->dispatch(new PostViewsIncrement($post))->onQueue('post');
        // 触发文章浏览事件（这还只是简单的事件监听处理，是同步的，不算队列，并发高会很卡）
        event(new PostViewed($post));
        return "Show Post #{$post->id}, Views: {$post->views}";
    }
    // 获取热门文章排行榜
    public function popular()
    {
        $posts = $this->postRepo->trending(10);
        // dump($posts);
        if ($posts) {
            dump($posts->toArray());
        }
    }
}
